$(function() {
	var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r) {
		var gps_w = r.point.lat;
		var gps_j = r.point.lng;
		$.ajax({
			url: 'https://newapp.icandrive.cn/api/hospital.php',
			type: 'post',
			dataType: 'json',
			success: function(data) {
				var map = new BMap.Map("allmap");
				map.centerAndZoom(new BMap.Point(gps_j, gps_w), 16);

				$(data.data).each(function() {

					var point = new BMap.Point(this.gps_j, this.gps_w);
					map.centerAndZoom(point, 16);
					var marker = new BMap.Marker(point); // 创建标注
					map.addOverlay(marker); // 将标注添加到地图中

					var label = new BMap.Label(this.hos_name, {
						offset: new BMap.Size(20, -10)
					});
					marker.setLabel(label);

				});
				var point = new BMap.Point(gps_j, gps_w);
				map.centerAndZoom(point, 16);
				var marker = new BMap.Marker(point); // 创建标注
				map.addOverlay(marker); // 将标注添加到地图中
				marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画

			},
			error: function() {

			}
		});

		// 百度地图API功能
		var map = new BMap.Map("allmap");
		map.centerAndZoom(new BMap.Point(116.404, 39.915), 14);
		var local = new BMap.LocalSearch(map, {
			renderOptions: {
				map: map
			}
		});
		local.searchInBounds("医院", map.getBounds());

		map.addEventListener("dragend", function() {
			map.clearOverlays();
			local.searchInBounds("医院", map.getBounds());
		});

	})

	/*监听手机返回按键*/
	document.addEventListener("plusready", function() {
		// 注册返回按键事件
		plus.key.addEventListener('backbutton', function() {
			window.history.back();
		}, false);
		//此处false代表不退出页面，如果为true回退到上一个一面
	});
});
